package com.asa.ui.frame;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.ImageIcon;

/**
 * 图像绘制的辅助工具类
 * 屏幕坐标和数学坐标是不同的
 * 我们如果希望像数学一样的使用代码绘制就要利用这个工具类
 * @author asa
 *
 */
public class AlgoVisHelper {
	
	
	private AlgoVisHelper() {}
	
	/**
	 * 设置画笔的粗细
	 * @param g2d环境
	 * @param w	粗细
	 */
	public static void setStrokeWidth(Graphics2D g2d,int w) {
		
		int strokeWiidth = w;
		g2d.setStroke(new BasicStroke(strokeWiidth,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
		
	}
	/**
	 * 设置颜色
	 * @param g2d
	 * @param color
	 */
	public static void setColor(Graphics2D g2d,Color color) {
		g2d.setColor(color);
	}
	
	/**
	 * 画空心圆，只有边
	 * @param g2d
	 * @param x
	 * @param y
	 * @param r
	 */
	public static void strokeCircle(Graphics2D g2d,int x,int y,int r) {
		
		Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
		g2d.draw(circle);
	}
	/**
	 * 画实心圆
	 * @param g2d
	 * @param x
	 * @param y
	 * @param r
	 */
	public static void fillCircle(Graphics2D g2d,int x,int y,int r) {
		
		Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
		g2d.fill(circle);
	}
	 //设置抗锯齿
    public static void setRenderingHints(Graphics2D g){
    	RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    	hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    	g.addRenderingHints(hints);
    }
	//填充矩形的辅助类
	public static void fillReactangle(Graphics2D g,int x,int y,int width,int height){
		Rectangle2D reactangle = new Rectangle2D.Double(x,y,width,height);
		g.fill(reactangle);
	}

	public static void pause(int t) {
		try {
			Thread.sleep(t);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//画图
    public static void drawImage(Graphics2D g,String imageUrl,int x,int y,int x1,int y1){
    	ImageIcon imageIcon=new ImageIcon(imageUrl);
    	Image image=imageIcon.getImage();
    	g.drawImage(image,x, y,x1,y1,null);
    }

	
  //写字
    /**
     * 
     * @param g
     * @param text
     * @param x	中心点
     * @param y	中心点
     */
    public static void drawString(Graphics2D g,String text,int x,int y){
    	
    	FontMetrics metrics = g.getFontMetrics();
    	int w = metrics.stringWidth(text);
    	int h = metrics.getDescent();
    	g.drawString(text, x - w/2, y+h);
    	
    	
    }

	
    
    
    
   public static void drawLine(Graphics2D g,int x1,int y1,int x2,int y2){
    	
	   g.drawLine(x1, y1, x2, y2);
	   
	   
    }
    
    
	
}
